package org.example.common.exception;

import org.example.lease.model.result.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/16 16:50
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler({
            ServerException.class,
            InsufficientDataException.class,
            ErrorResponseException.class,
            IOException.class,
            NoSuchAlgorithmException.class,
            InvalidKeyException.class,
            InvalidResponseException.class,
            XmlParserException.class,
            InternalException.class
    })
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> fileUploadException(Exception e){
        log.error("MinIO 异常", e);
        return Result.fail();
    }

    /**
     * 用户登录的时候各种参数校验可能会抛出的异常
     * @param e e
     * @return r
     */
    @ExceptionHandler(LeaseException.class)
    public Result<String> leaseException(LeaseException e){
        log.error("发生了LeaseException异常，异常信息为：{}", e.getMessage());
        return Result.ok(e.getMessage());
    }

    /**
     * 定义类型转换异常，将前端传递的status类型的参数，转换为java用的枚举类
     * @param e e
     * @return r
     */
    @ExceptionHandler(TypeConvertException.class)
    public Result<String> TypeConvertException(TypeConvertException e){
        log.info("发生了类型转换异常：{}", e.getMessage());
        return Result.ok(e.getMessage());
    }

}
